//
// Created by guanyicheng on 2022/9/14.
//
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
#include "../../common.h"

class Solution {

private:
    bool fun(TreeNode *node, int sum, int targetSum) {
        int s = node->val + sum;
        if (node->left == nullptr && node->right == nullptr) {
            return s == targetSum;
        }
        if (node->left && fun(node->left, s, targetSum)) {
            return true;
        }
        if (node->right && fun(node->right, s, targetSum)) {
            return true;
        }
        return false;
    }

public:
    bool hasPathSum(TreeNode* root, int targetSum) {
        if (root == nullptr) {
            return false;
        }
        return fun(root, 0, targetSum);
    }
};